<template>
	<m-layout2 title="选择城市">
		<uni-indexed-list class="list" :options="country" :showSelect="false" @click="bindClick" />
		<m-dialog :loading="isLoading" :showCancel="true" :isShow="setNameShow" @handleCancel="setNameShow = false"
			@handleConfirm="setCountry">
			<view class="setName-box">
				<view class="form-Item">
					454545}：{{tapItem}}
				</view>
				<view class="hint">
					<text>5454545</text>
				</view>
			</view>
		</m-dialog>
	</m-layout2>
</template>
<script>
	import { BindCountry } from '@/api/Users.js'
	const countrys = require('@/static/JSON/country.json')
	export default {
		data() {
			return {
				tapItem: '',
				setNameShow: false,
				isLoading: false,
				country: {},
				arrMap: {},
			};
		},
		computed: {
			locale() {
				return this.$store.state.setting.locale
			},
		},
		onLoad() {
			console.log(this.locale)
			this.groupNames(countrys);
		},
		methods: {
			async setCountry() {
				this.isLoading = true;
				const { success, msg } = await BindCountry({
					Country: this.tapItem
				})
				if (success) {
					this.sleep(1);
					this.isLoading = false;
					this.Jump()
				}
			},
			bindClick({ item: { name, key } }) {
				// this.tapItem = this.arrMap[key][name];
				this.tapItem = name;
				this.setNameShow = true;
			},
			groupNames(arr) {
				let acc2 = {};
				// const name = this.locale=='zhHans'?'name_cn':'name_en'
				const name = 'name_en'
				const map = arr.reduce((acc, val, idx) => {
					let char = val[name].charAt(0).toUpperCase();
					acc[char] = [].concat((acc[char] || []), val[name]);
					acc2[char] = { ...acc2[char] };
					acc2[char][val[name]] = val;
					return acc;
				}, {});
				this.arrMap = acc2;
				const res = Object.keys(map).map(el => ({
					letter: el,
					data: map[el]
				}));
				this.country = res;
			},
			
			
		},
		onPullDownRefresh() {
			this.reset();
		},
		onReachBottom() {
			this.getData();
			console.log("触底了");
		},
	}
</script>

<style lang="scss">
	.header {
		padding: 24rpx;
		background: var(--bg_block);
		;

		.h1 {
			display: block;
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
		}

		.h2 {
			font-size: 24rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #8F99BC;
		}
	}

	.setName-box {
		text-align: center;
		padding: 50rpx;

		.form-Item {
			font-size: 34rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
		}

		.hint {
			padding-top: 20rpx;
			font-size: 30rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: var(--text_1);
		}
	}

	.list {
		::v-deep .uni-indexed-list__title-wrapper {
			position: sticky;
			width: 100%;
			top: 0;
			z-index: 9;
			background-color: var(--bg_block);

			.uni-indexed-list__title {
				width: 100%;
				color: var(--text_thenme);
			}
		}

		::v-deep .uni-indexed-list__list {
			background-color: unset;
			border-top-color: var(--border);

			.uni-indexed-list__item-border {
				border-bottom-color: var(--border);

				.uni-indexed-list__item-content {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: var(--text_1);
				}
			}
		}

		.li {
			padding: 30rpx 0 24rpx;

			text {
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: var(--text_thenme);
			}
		}

		.li+.li {

			border-top: 1rpx solid var(--border);

		}
	}
</style>
